
package memoria;
import ahorcado.ahorcado;
import java.util.Random;
import javax.swing.*;
import java.awt.Image;
import java.util.Timer;
import java.util.TimerTask;

public class frmMemoria extends javax.swing.JFrame {
    int img,cant,gan;
    int[] num = new int[6];
    int[] cot = new int[12];
    int[] aux = new int[2];
    boolean[] est = new boolean[12];
    private Image imagen;
    int volt;
    int carp;
    
    private Timer tiempo ;
    private TimerTask task;
    private int speed = 1000;
    private int seg=0;
    private int min=0;
    boolean run = false;    
    
    public frmMemoria() {
        initComponents();
    }
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        lbltimer = new javax.swing.JLabel();
        btnJugar = new javax.swing.JButton();
        cmbTema = new javax.swing.JComboBox();
        Hangman = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Juego de memoria");
        setForeground(java.awt.Color.white);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/memoria/0/0.jpg"))); // NOI18N
        jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel1MouseClicked(evt);
            }
        });

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/memoria/0/0.jpg"))); // NOI18N
        jLabel2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel2MouseClicked(evt);
            }
        });

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/memoria/0/0.jpg"))); // NOI18N
        jLabel3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel3MouseClicked(evt);
            }
        });

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/memoria/0/0.jpg"))); // NOI18N
        jLabel4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel4MouseClicked(evt);
            }
        });

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/memoria/0/0.jpg"))); // NOI18N
        jLabel5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel5MouseClicked(evt);
            }
        });

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/memoria/0/0.jpg"))); // NOI18N
        jLabel6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel6MouseClicked(evt);
            }
        });

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/memoria/0/0.jpg"))); // NOI18N
        jLabel7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel7MouseClicked(evt);
            }
        });

        jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/memoria/0/0.jpg"))); // NOI18N
        jLabel8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel8MouseClicked(evt);
            }
        });

        jLabel9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/memoria/0/0.jpg"))); // NOI18N
        jLabel9.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel9MouseClicked(evt);
            }
        });

        jLabel10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/memoria/0/0.jpg"))); // NOI18N
        jLabel10.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel10MouseClicked(evt);
            }
        });

        jLabel11.setIcon(new javax.swing.ImageIcon(getClass().getResource("/memoria/0/0.jpg"))); // NOI18N
        jLabel11.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel11MouseClicked(evt);
            }
        });

        jLabel12.setIcon(new javax.swing.ImageIcon(getClass().getResource("/memoria/0/0.jpg"))); // NOI18N
        jLabel12.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel12MouseClicked(evt);
            }
        });

        jLabel13.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel13.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        lbltimer.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        btnJugar.setText("PLAY");
        btnJugar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnJugarActionPerformed(evt);
            }
        });

        cmbTema.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "verbs", "mau", "jil" }));

        Hangman.setText("Hangman");
        Hangman.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                HangmanActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(30, 30, 30)
                        .addComponent(jLabel6)
                        .addGap(34, 34, 34)
                        .addComponent(jLabel7)
                        .addGap(32, 32, 32)
                        .addComponent(jLabel8))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(134, 134, 134)
                        .addComponent(lbltimer, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(30, 30, 30)
                        .addComponent(jLabel2)
                        .addGap(34, 34, 34)
                        .addComponent(jLabel3)
                        .addGap(32, 32, 32)
                        .addComponent(jLabel4))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cmbTema, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(48, 48, 48)
                        .addComponent(btnJugar, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(Hangman)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel9)
                            .addGap(30, 30, 30)
                            .addComponent(jLabel10)
                            .addGap(34, 34, 34)
                            .addComponent(jLabel11)
                            .addGap(32, 32, 32)
                            .addComponent(jLabel12))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbTema, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnJugar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(lbltimer)
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel6)
                        .addComponent(jLabel5))
                    .addComponent(jLabel8))
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel11)
                    .addComponent(jLabel12)
                    .addComponent(jLabel10)
                    .addComponent(jLabel9))
                .addGap(20, 20, 20)
                .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Hangman)
                .addGap(17, 17, 17))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnJugarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnJugarActionPerformed
           iniciar();
           limpiar();
           carp = cmbTema.getSelectedIndex();
           btnJugar.setText("Jugar otra vez");
    }//GEN-LAST:event_btnJugarActionPerformed

    private void jLabel12MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel12MouseClicked
        if (volt==2){
            comprobacion();
        }
        volt++;
        aux[volt-1]=12; 
        jLabel12.setIcon(new ImageIcon(getClass().getResource("/memoria/"+carp+"/"+ cot[11]+".jpg")));
        ganar();
    }//GEN-LAST:event_jLabel12MouseClicked

    private void jLabel11MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel11MouseClicked
        if (volt==2){
            comprobacion();
        }
        volt++;
        aux[volt-1]=11; 
        jLabel11.setIcon(new ImageIcon(getClass().getResource("/memoria/"+carp+"/"+ cot[10]+".jpg")));
         ganar();
    }//GEN-LAST:event_jLabel11MouseClicked

    private void jLabel10MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel10MouseClicked
        if (volt==2){
            comprobacion();
        }
        volt++;
        aux[volt-1]=10;
        jLabel10.setIcon(new ImageIcon(getClass().getResource("/memoria/"+carp+"/"+ cot[9]+".jpg")));
         ganar();
    }//GEN-LAST:event_jLabel10MouseClicked

    private void jLabel9MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel9MouseClicked
        if (volt==2){
            comprobacion();
        }
        volt++;
        aux[volt-1]=9;
        jLabel9.setIcon(new ImageIcon(getClass().getResource("/memoria/"+carp+"/"+ cot[8]+".jpg")));
        ganar();
    }//GEN-LAST:event_jLabel9MouseClicked

    private void jLabel8MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel8MouseClicked
        if (volt==2){
            comprobacion();
        }
        volt++;
        aux[volt-1]=8;
        jLabel8.setIcon(new ImageIcon(getClass().getResource("/memoria/"+carp+"/"+ cot[7]+".jpg")));
        ganar();
    }//GEN-LAST:event_jLabel8MouseClicked

    private void jLabel7MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel7MouseClicked
        if (volt==2){
            comprobacion();
        }
        volt++;
        aux[volt-1]=7;
        jLabel7.setIcon(new ImageIcon(getClass().getResource("/memoria/"+carp+"/"+ cot[6]+".jpg")));
        ganar();
    }//GEN-LAST:event_jLabel7MouseClicked

    private void jLabel6MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel6MouseClicked
        if (volt==2){
            comprobacion();
        }
        volt++;
        aux[volt-1]=6;
        jLabel6.setIcon(new ImageIcon(getClass().getResource("/memoria/"+carp+"/"+ cot[5]+".jpg")));
        ganar();
    }//GEN-LAST:event_jLabel6MouseClicked

    private void jLabel5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel5MouseClicked
        if (volt==2){
            comprobacion();
        }
        volt++;
        aux[volt-1]=5;
        jLabel5.setIcon(new ImageIcon(getClass().getResource("/memoria/"+carp+"/"+ cot[4]+".jpg")));
        ganar();
    }//GEN-LAST:event_jLabel5MouseClicked

    private void jLabel4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel4MouseClicked
        if (volt==2){
            comprobacion();
        }
        volt++;
        aux[volt-1]=4;
        jLabel4.setIcon(new ImageIcon(getClass().getResource("/memoria/"+carp+"/"+ cot[3]+".jpg")));
        ganar();
    }//GEN-LAST:event_jLabel4MouseClicked

    private void jLabel3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel3MouseClicked
        if (volt==2){
            comprobacion();
        }
        volt++;
        aux[volt-1]=3;
        jLabel3.setIcon(new ImageIcon(getClass().getResource("/memoria/"+carp+"/"+ cot[2]+".jpg")));
        ganar();
    }//GEN-LAST:event_jLabel3MouseClicked

    private void jLabel2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel2MouseClicked
        if (volt==2){
            comprobacion();
        }
        volt++;
        aux[volt-1]=2;
        jLabel2.setIcon(new ImageIcon(getClass().getResource("/memoria/"+carp+"/"+ cot[1]+".jpg")));
        ganar();
    }//GEN-LAST:event_jLabel2MouseClicked

    private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel1MouseClicked
        if (volt==2){
            comprobacion();
        }
        volt++;
        aux[volt-1]=1;
        jLabel1.setIcon(new ImageIcon(getClass().getResource("/memoria/"+carp+"/"+ cot[0]+".jpg")));
        ganar();
    }//GEN-LAST:event_jLabel1MouseClicked

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        carp=0;
    }//GEN-LAST:event_formWindowOpened

    private void HangmanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_HangmanActionPerformed
        ahorcado ahoracado= new ahorcado();
        ahoracado.setVisible(true);
    }//GEN-LAST:event_HangmanActionPerformed
    
    private void iniciar(){
        btnJugar.setVisible(false);
        for(int x=0;x<6;x++)
            num[x]=0;
        for(int x=0;x<12;x++)
            est[x]=true;
        aleatorio();
        volt=0;
        gan=0;
        ///////////////////////////////
           run=true;
           tiempo = new Timer();
           task = new TimerTask() {               
               public void run() {
                   seg++;                   
                   if (seg<=60){
                       if(seg<10){
                           lbltimer.setText("Time: "+String.valueOf(min) +":0"+String.valueOf(seg));
                       }else{
                        lbltimer.setText("Time: "+String.valueOf(min) +":"+String.valueOf(seg));}
                   }
                   else{seg=0;min++;}
                    }
           };                                         
           tiempo.schedule(task,0,speed);
        ///////////////////////////////////
           
    }
    
    private void aleatorio(){
        Random r = new Random();
        int x;
        x=0;
        for(int cant=0;cant<12;cant++){
            img = r.nextInt(6);
            if (num[img]<2){
                num[img]++;
                cot[cant]=img+1;
            }else{
                cant--;
            }
        }
    }
    
    private void comprobacion(){
        if(cot[aux[0]-1]==cot[aux[1]-1]){
            est[aux[0]-1]=false;
            est[aux[1]-1]=false;
            volt=0;
            gan++;
        }else{
            volt=0;
            voltear(aux[0]);
            voltear(aux[1]);
        }
    }
    
    private void ganar(){
         if ((volt==2)&&(gan==5)){
            jLabel13.setText("ganaste, "+lbltimer.getText());
            tiempo.cancel();
            task.cancel();
            run=false;
            min=0; seg=0;
            btnJugar.setVisible(true);
         }
    }
    
    private void limpiar(){
                jLabel13.setText("");
                jLabel1.setIcon(new ImageIcon(getClass().getResource("/memoria/0/0.jpg")));
                jLabel2.setIcon(new ImageIcon(getClass().getResource("/memoria/0/0.jpg")));
                jLabel3.setIcon(new ImageIcon(getClass().getResource("/memoria/0/0.jpg")));
                jLabel4.setIcon(new ImageIcon(getClass().getResource("/memoria/0/0.jpg")));
                jLabel5.setIcon(new ImageIcon(getClass().getResource("/memoria/0/0.jpg")));
                jLabel6.setIcon(new ImageIcon(getClass().getResource("/memoria/0/0.jpg")));
                jLabel7.setIcon(new ImageIcon(getClass().getResource("/memoria/0/0.jpg")));
                jLabel8.setIcon(new ImageIcon(getClass().getResource("/memoria/0/0.jpg")));
                jLabel9.setIcon(new ImageIcon(getClass().getResource("/memoria/0/0.jpg")));
                jLabel10.setIcon(new ImageIcon(getClass().getResource("/memoria/0/0.jpg")));
                jLabel11.setIcon(new ImageIcon(getClass().getResource("/memoria/0/0.jpg")));
                jLabel12.setIcon(new ImageIcon(getClass().getResource("/memoria/0/0.jpg")));
    }
    
    private void voltear(int z){
        switch(z){
            case 1:
                jLabel1.setIcon(new ImageIcon(getClass().getResource("/memoria/0/0.jpg")));
                break;
            case 2:
                jLabel2.setIcon(new ImageIcon(getClass().getResource("/memoria/0/0.jpg")));
                break;
            case 3:
                jLabel3.setIcon(new ImageIcon(getClass().getResource("/memoria/0/0.jpg")));
                break;
            case 4:
                jLabel4.setIcon(new ImageIcon(getClass().getResource("/memoria/0/0.jpg")));
                break;
            case 5:
                jLabel5.setIcon(new ImageIcon(getClass().getResource("/memoria/0/0.jpg")));
                break;
            case 6:
                jLabel6.setIcon(new ImageIcon(getClass().getResource("/memoria/0/0.jpg")));
                break;
            case 7:
                jLabel7.setIcon(new ImageIcon(getClass().getResource("/memoria/0/0.jpg")));
                break;
            case 8:
                jLabel8.setIcon(new ImageIcon(getClass().getResource("/memoria/0/0.jpg")));
                break;
            case 9:
                jLabel9.setIcon(new ImageIcon(getClass().getResource("/memoria/0/0.jpg")));
                break;
            case 10:
                jLabel10.setIcon(new ImageIcon(getClass().getResource("/memoria/0/0.jpg")));
                break;
            case 11:
                jLabel11.setIcon(new ImageIcon(getClass().getResource("/memoria/0/0.jpg")));
                break;
            case 12:
                jLabel12.setIcon(new ImageIcon(getClass().getResource("/memoria/0/0.jpg")));
                break;
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frmMemoria().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Hangman;
    private javax.swing.JButton btnJugar;
    private javax.swing.JComboBox cmbTema;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel lbltimer;
    // End of variables declaration//GEN-END:variables
    
}
